/*  Game/Army Editor for yALB - Yet Another List Builder (roster Editor for Wargames/tabletop games).
    Copyright (C) 2008-2012  Daniel Ricardo Castro Alvarado

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.*/

#include "unittreewidget.h"

#include <QDebug>

UnitTreeWidget::UnitTreeWidget(QWidget *parent) :
    QTreeWidget(parent)
{
    this->setEditTriggers(QAbstractItemView::AllEditTriggers);
}

QModelIndex UnitTreeWidget::moveCursor(CursorAction cursorAction, Qt::KeyboardModifiers modifiers) {
    if(cursorAction == QAbstractItemView::MoveNext || cursorAction == QAbstractItemView::MoveRight) {
        QModelIndex i = currentIndex();
        if(i.column() < columnCount()-1)
            return this->model()->index(i.row(), i.column() +1, i.parent());
        setCurrentIndex(model()->index(i.row(), 0, i.parent()));
    } else if(cursorAction == QAbstractItemView::MovePrevious || cursorAction == QAbstractItemView::MoveLeft) {
        QModelIndex i = currentIndex();
        if(i.column() > 1)
            return this->model()->index(i.row(), i.column() -1, i.parent());
        setCurrentIndex(model()->index(i.row(), columnCount() - 1, i.parent()));
    }
    return QTreeWidget::moveCursor(cursorAction, modifiers);
}

void UnitTreeWidget::commitData(QWidget *editor) {
    QModelIndex index = currentIndex();
    QString oldText =index.data().toString();

    QTreeView::commitData(editor);
    QString newText = index.data().toString();

    if(oldText != newText)
        emit userChangedText(currentItem(), index.column());
}
